package cn.webrx;

import java.util.*;

public class HashMap2 {
    public static void main(String[] args) {
        HashMap<Integer, Student> hms = new HashMap<>();
        hms.put(100, new Student(101, "李四"));
        hms.put(200, new Student(202, "张三丰"));
        hms.put(300, new Student(303, "李四"));
        //System.out.println(hms.get(400));
        //System.out.println(hms.getOrDefault(400, null));

        //遍历
        //hms.keySet() //Set<Integer>
        //hms.values() //Collection<Student>
        //hms.entrySet() //Set<Entry<Integer,Student>>

        //迭代器 for 循环  .forEach(lambda表达式)

        //01
        //Iterator<Map.Entry<Integer, Student>> it = hms.entrySet().iterator();
        //while(it.hasNext()){
            //System.out.println(it.next());
            //Map.Entry<Integer,Student> entry = it.next();
            //System.out.println(entry.getKey());
            //System.out.println(it.next().getValue().getName());
        //}

        //02
        Set<Integer> keys = hms.keySet();
        Iterator<Integer> it2 = keys.iterator();
        //while(it2.hasNext()){
        //    Integer key = it2.next();
        //    System.out.println(key);
        //    System.out.println(hms.get(key));
        //}

        //03
        Iterator<Student> it3 = hms.values().iterator();
        //while(it3.hasNext()){
        //    System.out.println(it3.next().getId());
        //}

        Collection<Student> cs = hms.values();
        for(Student st : cs){
            System.out.println(st);
        }

        for(Integer k : hms.keySet()){
            System.out.println(k);
            System.out.println(hms.get(k));
        }

        System.out.println("------------------");
        for(var e : hms.entrySet()){
            Integer k = e.getKey();
            Student s = e.getValue();
            System.out.println(k);
            System.out.println(s);
            System.out.println(s.getName());
        }


    }
}
